package com.yanqu.road.dao.impl.activity.entername;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.entername.ChannelFightMatchRule;
import com.yanqu.road.entity.activity.entername.ChannelFightZone;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class ChannelEnterNameConfigDaoImpl  extends TempDao {

    private static String getChannelFightZoneSql = "select * from `t_n_areafight_zone` where `activityId`=? ORDER BY `areaId` ASC";
    public List<ChannelFightZone> getChannelFightZoneList(int activityId) {
        List<ChannelFightZone> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getChannelCenterDb());
            call = getDb().getConnection().prepareCall(getChannelFightZoneSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ChannelFightZone data = new ChannelFightZone();
                data.setActivityId(rs.getInt("activityId"));
                data.setAreaId(rs.getInt("areaId"));
                data.setName(rs.getString("name"));
                data.setAreaMember(rs.getString("areaMember"));
                data.setZoneList(StringUtils.stringToIntegerList(data.getAreaMember(), "\\|"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    private static String getChannelFightMatchRuleSql = "select * from `t_n_areafight_matchrule` where `activityId`=? ORDER BY `groupId` ASC";
    public List<ChannelFightMatchRule> getChannelFightMatchRuleList(int activityId) {
        List<ChannelFightMatchRule> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getChannelCenterDb());
            call = getDb().getConnection().prepareCall(getChannelFightMatchRuleSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                ChannelFightMatchRule data = new ChannelFightMatchRule();
                data.setActivityId(rs.getInt("activityId"));
                data.setGroupId(rs.getInt("groupId"));
                data.setRankRange(rs.getString("rankRange"));
                list.add(data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }
}
